package ecmnext.utils.gui;

/***************************************************************/
/*                     SOFTWARE SECTION                        */
/***************************************************************/
/*
 * <p>Name: ECMNext</p>
 * <p>Description: Simulation of the platform of trading Software</p>
 * <p>Licence: General Public License</p>
 * <p>Organisation: EC Marseille</p>
 * <p>Persons involved in the project : groupe Plateforme de Trading</p>
 * <p>Current version: 1.0</p>
 */


/***************************************************************/
/*                      IMPORT SECTION                         */
/***************************************************************/

import java.io.*;
import java.net.*;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import ecmnext.utils.*;
import java.util.logging.Logger;
//import jessx.applet.FilesLoaded;

/***************************************************************/
/*               AbstractAboutBox CLASS SECTION                */
/***************************************************************/

/**
 * <p>Title: AbstractAboutBox</p>
 * <p>Description: This class implements an about box, that will typically be
 * displayed by the frame class when the user click on the about button from
 * the menu.
 * It gives some version info and a link to a tutorial for using / programming
 * new module.</p>
 * @author Xuewen YANG ET Antoine COUDERC
 * @version 1.4
 */

public abstract class AbstractAboutBox
    extends JDialog implements ActionListener, Constants {

  JTabbedPane jTabbedPane1 = new JTabbedPane();
  JPanel panelTeam = new JPanel();
  JPanel panelVersion = new JPanel();
  JPanel panelLicence = new JPanel();
  JPanel panelModulesInfo = new JPanel();

  ImageIcon logoECMNext = new ImageIcon();
  ImageIcon logoECLille = new ImageIcon();
  ImageIcon logoUSTL = new ImageIcon();
  ImageIcon logoEcoxp = new ImageIcon();
  ImageIcon logoTrading = new ImageIcon();
  ImageIcon logoMarket = new ImageIcon();
  ImageIcon logoTrobot = new ImageIcon();

  JTextField jTextFieldProductName = new JTextField("ECMNext: Simulation d'une plateform de Trading");
  JTextField jTextFieldVersion = new JTextField("Version : "+Constants.VERSION);
  JTextField jTextFieldDate = new JTextField(Constants.DATE_OF_RELEASE);
  JTextField jTextFieldRessources = new JTextField("Ressources and tutorials at " +
                              Constants.WEB_PAGE);


  JLabel jLabelTeam = new JLabel();



  JLabel jLabelLogoTrading = new JLabel();
  JLabel jLabelLogoMarket = new JLabel();
  JLabel jLabelECMNextLogo = new JLabel();

  JLabel jLabelLogoTrobot = new JLabel();

  JTextArea jTextAreaModulesAnalysis = new JTextArea();

  JScrollPane jScrollPane1; 
  JScrollPane jScrollPane2 = new JScrollPane();

  GridBagLayout gridBagLayout1 = new GridBagLayout();
  GridBagLayout gridBagLayout2 = new GridBagLayout();
  GridBagLayout gridBagLayout3 = new GridBagLayout();
  GridBagLayout gridBagLayout4 = new GridBagLayout();

  String fileSeparator = System.getProperty("file.separator");

  public AbstractAboutBox(Frame parent) {
    super(parent);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {
    jTextFieldProductName.setBorder(null);
    jTextFieldProductName.setOpaque(false);
    jTextFieldProductName.setEditable(false);
    jTextFieldProductName.setHorizontalAlignment(SwingConstants.CENTER);

    jTextFieldVersion.setBorder(null);
    jTextFieldVersion.setOpaque(false);
    jTextFieldVersion.setEditable(false);
    jTextFieldVersion.setHorizontalAlignment(SwingConstants.CENTER);

    jTextFieldDate.setBorder(null);
    jTextFieldDate.setOpaque(false);
    jTextFieldDate.setEditable(false);
    jTextFieldDate.setHorizontalAlignment(SwingConstants.CENTER);

    jTextFieldRessources.setBorder(null);
    jTextFieldRessources.setOpaque(false);
    jTextFieldRessources.setEditable(false);
    jTextFieldRessources.setHorizontalAlignment(SwingConstants.CENTER);



    jTabbedPane1.setMinimumSize(new Dimension(560, 580));
    jTabbedPane1.setPreferredSize(new Dimension(560, 580));
    panelLicence.setLayout(gridBagLayout3);
    panelVersion.setLayout(gridBagLayout4);

    jScrollPane2.getViewport().add(this.getLicense());

     
     //AnalysisToolsCore.logger.info("Displaying analyzer about box.");
     String imagesDir = System.getProperty("user.dir") + fileSeparator +
         "images" + fileSeparator;

     //AnalysisToolsCore.logger.debug("Getting images...");
     logoECMNext = new ImageIcon(new URL("file:" + imagesDir +
                                       "logo_ECMNext_small.PNG"));

     logoTrading = new ImageIcon(new URL("file:" + imagesDir +
                                         "logo_trading.PNG"));
     //AnalysisToolsCore.logger.info("- file:" + imagesDir + "trading_small.PNG");
     logoMarket = new ImageIcon(new URL("file:" + imagesDir +
                                         "market_logo.PNG"));
     logoTrobot = new ImageIcon(new URL("file:" + imagesDir +
     "Trobot_logo.JPG"));
//fin des ajouts

    this.setTitle("About ECMNext");
    this.getContentPane().setLayout(gridBagLayout2);
    panelTeam.setLayout(gridBagLayout1);

    jLabelECMNextLogo.setIcon(logoECMNext);



    jLabelTeam.setText("<html><body><p><h1>Tuteurs</h1>François BRUCKER, " +
    		"Christophe POUET</p><p><h1>Development Goup</h1>"+
    		"<u>Maxime</u><br>Antonie<br>Fostine<br></p>"+
    		"<p><u>Liang CHEN, Viephone</u><br>Xuewen YANG et Antoine COUDERC<br></body></html>");
    jLabelTeam.setVerticalAlignment(SwingConstants.TOP);
    jLabelTeam.setHorizontalAlignment(SwingConstants.CENTER);
    jLabelLogoTrading.setIcon(logoTrading);
    jLabelLogoMarket.setIcon(logoMarket);
    jLabelLogoTrobot.setIcon(logoTrobot);

    this.getContentPane().add(jTabbedPane1,
                              new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));
    jTabbedPane1.add(panelVersion,"Version");
    jTabbedPane1.add(panelTeam,"Team");
    jTabbedPane1.add(panelLicence,"License");
    panelLicence.add(jScrollPane2, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));
    panelVersion.add(jTextFieldRessources,
                     new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
                                            , GridBagConstraints.CENTER,
                                            GridBagConstraints.BOTH,
                                            new Insets(5, 0, 5, 0), 0, 0));
    panelVersion.add(jTextFieldDate, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(5, 0, 5, 0), 0, 0));
    panelVersion.add(jTextFieldVersion, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(5, 0, 5, 0), 0, 0));
    panelVersion.add(jTextFieldProductName,
                     new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
                                            , GridBagConstraints.CENTER,
                                            GridBagConstraints.BOTH,
                                            new Insets(5, 0, 5, 0), 0, 0));
    panelVersion.add(jLabelECMNextLogo,
                     new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0
                                            , GridBagConstraints.CENTER,
                                            GridBagConstraints.NONE,
                                            new Insets(5, 5, 5, 5), 0, 0));
    panelTeam.add(jLabelLogoTrading,
                  new GridBagConstraints(1, 8, 1, 1, 1.0, 1.0
                                         , GridBagConstraints.CENTER,
                                         GridBagConstraints.NONE,
                                         new Insets(0, 0, 0, 5), 0, 0));
    panelTeam.add(jLabelLogoMarket,
                  new GridBagConstraints(2, 8, 1, 1, 1.0, 1.0
                                         , GridBagConstraints.CENTER,
                                         GridBagConstraints.NONE,
                                         new Insets(0, 0, 0, 5), 0, 0));

    panelTeam.add(jLabelLogoTrobot,
            new GridBagConstraints(3, 8, 1, 1, 1.0, 1.0
                                   , GridBagConstraints.CENTER,
                                   GridBagConstraints.NONE,
                                   new Insets(0, 0, 0, 5), 0, 0));
    
    
   /* panelTeam.add(jTextFieldName6, new GridBagConstraints(0, 5, 5, 1, 1.0, 0.1
        , GridBagConstraints.WEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 5, 0), 0, 0));
    panelTeam.add(jTextFieldName7, new GridBagConstraints(0, 6, 5, 1, 1.0, 0.1
        , GridBagConstraints.WEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 5, 0), 0, 0));
    panelTeam.add(jTextFieldName8, new GridBagConstraints(0, 7, 5, 1, 1.0, 0.1
        , GridBagConstraints.WEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 5, 0), 0, 0));*/

    panelTeam.add(jLabelTeam, new GridBagConstraints(0, 0, 5, 1, 1.0, 0.1
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 5, 0), 0, 0));
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    dispose();
    }
    super.processWindowEvent(e);
  }

  /**
   * implement this function to load into the textarea loaded modules infos
   */
  public abstract void loadModulesInfo();

  //Close the dialog on a button event
  public void actionPerformed(ActionEvent e) {
    dispose();
  }

  /*
   * modifs tests
  private JEditorPane getLicense() {
      JEditorPane license = FilesLoaded.getJEditorPane();
      return license;
  }
  */
  
  private JEditorPane getLicense() {
	    String directory = System.getProperty("user.dir");
	    directory =directory.subSequence(0, directory.lastIndexOf(fileSeparator) + 1).toString();
	    JEditorPane license=new JEditorPane();
	    try {
	      license = new JEditorPane(new URL("file:" + directory +
	          "GNU GENERAL PUBLIC LICENSE.txt"));
	      license.setEditable(false);
	    }
	    catch (IOException ex1) {
	      Utils.logger.error("GENERAL PUBLIC LICENSE.txt not found.");
	      
	    }
	    return license;
	  }
  //fin des modifs tests

  public void Analysis_AboutBoxEnabled() {
    jTextAreaModulesAnalysis.setBackground(SystemColor.inactiveCaptionText);
    jTextAreaModulesAnalysis.setBackground(new Color(255, 255, 255));
    jTextAreaModulesAnalysis.setFont(new java.awt.Font("Lucida Console", 0, 12));
    jTextAreaModulesAnalysis.setEditable(false);
    jTextAreaModulesAnalysis.setLineWrap(true);
    jTextAreaModulesAnalysis.setWrapStyleWord(true);

    this.loadModulesInfo();
    
    jTabbedPane1.add(panelModulesInfo,"Modules Information");
    jScrollPane1 = new JScrollPane();
    jScrollPane1.setPreferredSize(new Dimension(400,400));
    panelModulesInfo.add(jScrollPane1, new GridBagConstraints(1, 5, 1, 1, 1, 1
        , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(5, 5, 5, 5), 0, 0));

    jScrollPane1.getViewport().add(jTextAreaModulesAnalysis);
  }

  public JTextArea getModulesInfoTextArea() {
    return this.jTextAreaModulesAnalysis;
  }

}
